using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace SqlScriptCreator.Generators
{


    class TableDataScriptGenerator : IScriptGenerator
    {


        private String _tableName = "myTable";

        [CategoryAttribute("Table settings"),
         DescriptionAttribute("The name of the insert table for the generated inserts.")]
        public string TableName
        {
            get
            {
                return _tableName;
            }
            set
            {
                _tableName = value;
            }
        }

        private String _ignoreIdColumn = "ID";

        [CategoryAttribute("Table settings"),
         DescriptionAttribute("The name of the column which will be hidden from the insert statements.")]
        public string IgnoreIdColumn
        {
            get
            {
                return _ignoreIdColumn;
            }
            set
            {
                _ignoreIdColumn = value;
            }
        }


        #region IScriptGenerator Members

        override public string ToString()
        {
            return "Table data script";
        }

        public string generateScript(string query, DataTable dtQueryResult, DataTable dtParameters)
        {
            StringBuilder sb = new StringBuilder();
            string[] columnNames = new string[dtParameters.Rows.Count];
            string[] columnDataTypes = new string[dtParameters.Rows.Count];

            if (dtQueryResult != null)
            {
                int columns = 0;
                for (int i = 0; i < dtParameters.Rows.Count; i++)
                {
                    DataRow row = dtParameters.Rows[i];
                    String dataType = row["DataTypeName"].ToString();
                    String columnName = row["BaseColumnName"].ToString();

                    if (!columnName.Equals(IgnoreIdColumn, StringComparison.CurrentCultureIgnoreCase))
                    {
                        columnNames[columns] = columnName;
                        columnDataTypes[columns] = dataType;
                        columns++;
                    }
                }

                DataTable dtData = dtQueryResult;
                foreach (DataRow row in dtData.Rows)
                {
                    sb.Append("INSERT INTO @" + TableName + " (");
                    sb.Append(String.Join(", ", columnNames,0,columns));
                    sb.AppendLine(")");
                    sb.Append("VALUES (");

                    for (int i = 0; i < columns; i++)
                    {
                        sb.Append(SqlTools.getSqlDataRepresentation(row[columnNames[i]], columnDataTypes[i]));
                        if (i + 1 < columns) sb.Append(", "); 
                    }
                    
                    sb.AppendLine(")");
                }
            }
            return sb.ToString();	
        }

        #endregion
    }
}
